package com.jyw.business.ins.controller;

import com.github.pagehelper.PageHelper;
import com.jyw.business.ast.domain.AstInfo;
import com.jyw.business.ast.service.IAstInfoService;
import com.jyw.business.ins.domain.InsPlan;
import com.jyw.business.ins.domain.InsUnit;
import com.jyw.business.ins.domain.dto.web.*;
import com.jyw.business.ins.domain.vo.web.InsPlanAstPageVO;
import com.jyw.business.ins.domain.vo.web.InsPlanInfoVO;
import com.jyw.business.ins.domain.vo.web.InsPlanItemVO;
import com.jyw.business.ins.domain.vo.web.InsPlanUnitPageVO;
import com.jyw.business.ins.service.IInsConfigItemService;
import com.jyw.business.ins.service.IInsPlanItemService;
import com.jyw.business.ins.service.IInsPlanService;
import com.jyw.business.ins.service.IInsUnitService;
import com.jyw.common.annotation.Log;
import com.jyw.common.core.controller.BaseController;
import com.jyw.common.core.domain.CommonResult;
import com.jyw.common.core.page.TableDataInfo;
import com.jyw.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 巡检计划Controller
 *
 * @author zhangcheng
 * @date 2023-09-28
 */
@Api(tags = "巡检计划")
@RestController
@RequestMapping("/inspection/plan")
public class InsPlanController extends BaseController {
    @Resource
    private IInsPlanService insPlanService;

    @Resource
    private IInsUnitService insUnitService;

    @Resource
    private IAstInfoService astInfoService;

    @Resource
    private IInsPlanItemService insPlanItemService;

    @Resource
    private IInsConfigItemService insConfigItemService;

    /**
     * 巡检计划资产分页查询
     */
    @ApiOperation("巡检计划资产分页查询")
    @PreAuthorize("@ss.hasPermi('inspection:plan:planAstList')")
    @PostMapping("/planAstList")
    public TableDataInfo<InsPlan> planAstList(@RequestBody InsPlanAstPageDto pageDto) {
        PageHelper.startPage(pageDto.getPageNum(), pageDto.getPageSize()).reasonable(true);
        List<InsPlanAstPageVO> list = insPlanService.pagePlanAstList(pageDto);
        return getDataTable(list);
    }

    /**
     * 巡检计划单元分页查询
     */
    @ApiOperation("巡检计划单元分页查询")
    @PreAuthorize("@ss.hasPermi('inspection:plan:planUnitList')")
    @PostMapping("/planUnitList")
    public TableDataInfo<InsPlan> planUnitList(@RequestBody InsPlanUnitPageDto pageDto) {
        PageHelper.startPage(pageDto.getPageNum(), pageDto.getPageSize()).reasonable(true);
        List<InsPlanUnitPageVO> list = insPlanService.pagePlanUnitList(pageDto);
        return getDataTable(list);
    }

    /**
     * 查询巡检计划选择单元
     */
    @ApiOperation("查询巡检计划选择单元")
    @GetMapping("/unitList")
    public CommonResult<List<InsUnit>> unitList() {
        List<InsUnit> unitList = insUnitService.selectPlanUnitList();
        return CommonResult.success(unitList);
    }

    /**
     * 查询巡检计划选择单元
     */
    @ApiOperation("查询巡检计划选择单元")
    @GetMapping("/unitAllList")
    public CommonResult<List<InsUnit>> unitAllList() {
        List<InsUnit> unitList = insUnitService.list();
        return CommonResult.success(unitList);
    }

    /**
     * 查询巡检计划选择资产
     */
    @ApiOperation("查询巡检计划选择资产")
    @GetMapping("/astList")
    public CommonResult<List<AstInfo>> astList() {
        List<AstInfo> astList = astInfoService.selectPlanAstList();
        return CommonResult.success(astList);
    }

    /**
     * 查询巡检计划选择资产
     */
    @ApiOperation("查询巡检计划选择资产")
    @GetMapping("/astAllList")
    public CommonResult<List<AstInfo>> astAllList() {
        List<AstInfo> astList = astInfoService.list();
        return CommonResult.success(astList);
    }

    /**
     * 获取巡检计划详细信息
     */
    @ApiOperation("获取巡检计划详细信息")
    @GetMapping(value = "/{id}")
    public CommonResult<InsPlanInfoVO> getInfo(@PathVariable("id") Long id) {
        InsPlanInfoVO insPlanInfoVO = insPlanService.selectInfoPlanById(id);
        return CommonResult.success(insPlanInfoVO);
    }

    /**
     * 获取资产单元配置项
     */
    @ApiOperation("获取资产单元配置项")
    @GetMapping(value = "/getUnitItemList/{id}")
    public CommonResult<InsPlanInfoVO> getUnitItemList(@PathVariable("id") Long id) {
        List<InsPlanItemVO> insConfigItems = insConfigItemService.getItemList(id, 2);
        return CommonResult.success(insConfigItems);
    }

    /**
     * 获取资产单元配置项
     */
    @ApiOperation("获取资产单元配置项")
    @GetMapping(value = "/getAstItemList/{id}")
    public CommonResult<List<InsPlanItemVO>> getAstItemList(@PathVariable("id") Long id) {
        List<InsPlanItemVO> insConfigItems = insConfigItemService.getItemList(id, 1);
        return CommonResult.success(insConfigItems);
    }

    /**
     * 新增巡检计划
     */
    @ApiOperation("新增巡检计划")
    @PreAuthorize("@ss.hasPermi('inspection:plan:add')")
    @Log(title = "巡检计划", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public CommonResult add(@RequestBody @Valid InsPlanAddDto insPlanAddDto) {
        try {
            insPlanService.addInsPlan(insPlanAddDto);
        } catch (DuplicateKeyException e) {
            CommonResult.error("巡检资产/单元对应计划已存在！");
        }
        return CommonResult.success();
    }

    /**
     * 修改巡检计划
     */
    @ApiOperation("修改巡检计划")
    @PreAuthorize("@ss.hasPermi('inspection:plan:edit')")
    @Log(title = "巡检计划", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public CommonResult edit(@RequestBody @Valid InsPlanEditDto insPlanEditDto) {
        try {
            insPlanService.editInsPlan(insPlanEditDto);
        } catch (DuplicateKeyException e) {
            CommonResult.error("巡检资产/单元对应计划已存在！");
        }
        return CommonResult.success();
    }

    /**
     * 修改巡检计划状态
     */
    @ApiOperation("修改巡检计划状态")
    @PreAuthorize("@ss.hasPermi('inspection:plan:editState')")
    @Log(title = "巡检计划", businessType = BusinessType.UPDATE)
    @PutMapping("/editState")
    public CommonResult editState(@RequestBody @Valid InsPlanStateDto insUnitPlanDto) {
        return CommonResult.success(insPlanService.updateState(insUnitPlanDto));
    }

    /**
     * 分页查询巡检计划选择配置项
     */
    @ApiOperation("分页查询巡检计划选择配置项")
    @GetMapping("/planItemList")
    public TableDataInfo<InsPlanItemVO> planItemList(InsPlanItemPageDto insPlanItemPageDto) {
        startPage();
        List<InsPlanItemVO> list = insPlanItemService.pagePlanItemList(insPlanItemPageDto);
        return getDataTable(list);
    }

    /**
     * 删除巡检计划巡检项内容
     */
    @ApiOperation("删除巡检计划巡检项内容")
    @PreAuthorize("@ss.hasPermi('inspection:plan:itemRemove')")
    @Log(title = "删除巡检计划巡检项内容", businessType = BusinessType.DELETE)
    @DeleteMapping("itemRemove/{id}")
    public CommonResult itemRemove(@PathVariable Long id) {
        return CommonResult.success(insPlanItemService.deleteInsPlanItemById(id));
    }
}
